package com.bw.live

import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.OutlinedTextField
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.constraintlayout.compose.ConstraintLayout
import coil.compose.AsyncImage
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.live.common.ConstLive

data class LiveEntity(val id: Int, val name: String, val path: String, val num: Int)

@RequiresApi(Build.VERSION_CODES.R)
@Composable
fun LivePage() {
    val list = remember {
        SnapshotStateList<LiveEntity>()
    }

    val img_header =
        "https://sf3-cdn-tos.bdxiguastatic.com/img/pgc-image/0ab13d95b5ea4255b8761be0de22c7ca~300x300.image"
    list.add(
        LiveEntity(
            1,
            "Wandy",
            "https://p1-dy.bytexservice.com/img/user-avatar/4b59e33dc88e2a7a537bfffbd5d1141a~300x300.image",
            30
        )
    )
    list.add(
        LiveEntity(
            2,
            "Teddy",
            "https://p3.bdxiguaimg.com/img/mosaic-legacy/6ee900022f6451ca01eb~0x0.image",
            40
        )
    )
    list.add(
        LiveEntity(
            3,
            "Toney",
            "https://sf6-cdn-tos.bdxiguastatic.com/img/user-avatar/7e60eacf9bb39d1418c49c0f0985d7d0~300x300.image",
            50
        )
    )
    list.add(
        LiveEntity(
            4,
            "any",
            "https://sf3-cdn-tos.bdxiguastatic.com/img/pgc-image/0ab13d95b5ea4255b8761be0de22c7ca~300x300.image",
            50
        )
    )
    list.add(
        LiveEntity(
            5,
            "Toney",
            "https://sf6-cdn-tos.bdxiguastatic.com/img/user-avatar/7e60eacf9bb39d1418c49c0f0985d7d0~300x300.image",
            50
        )
    )
    list.add(
        LiveEntity(
            6,
            "Toney",
            "https://sf6-cdn-tos.bdxiguastatic.com/img/user-avatar/7e60eacf9bb39d1418c49c0f0985d7d0~300x300.image",
            50
        )
    )
    list.add(
        LiveEntity(
            7,
            "Toney",
            "https://sf6-cdn-tos.bdxiguastatic.com/img/user-avatar/7e60eacf9bb39d1418c49c0f0985d7d0~300x300.image",
            50
        )
    )
    list.add(
        LiveEntity(
            8,
            "Toney",
            "https://sf6-cdn-tos.bdxiguastatic.com/img/user-avatar/7e60eacf9bb39d1418c49c0f0985d7d0~300x300.image",
            50
        )
    )

    var search by remember {
        mutableStateOf("")
    }

    Column(Modifier.fillMaxSize()) {
        //约束布局
        ConstraintLayout {
            val (text, edit, icon) = createRefs() // 创建引用
            Text(text = "直播", fontSize = 18.sp, color = Color.Red, fontWeight = FontWeight.Bold,
                modifier = Modifier.constrainAs(text) {
                    top.linkTo(parent.top, 5.dp)
                    start.linkTo(parent.start, 10.dp)
                    bottom.linkTo(parent.bottom, 5.dp)
                })

            OutlinedTextField(
                value = search,
                onValueChange = { search = it },
                placeholder = {
                    Text(text = "开启直播之旅")
                },
                trailingIcon = {
                    Image(
                        painter = painterResource(id = R.drawable.ic_search),
                        contentDescription = ""
                    )
                },
                modifier = Modifier
                    .constrainAs(edit) {
                        top.linkTo(parent.top, 5.dp)
                        start.linkTo(text.end, 5.dp)
                        bottom.linkTo(parent.bottom, 5.dp)
                    },

                shape = RoundedCornerShape(60),
            )

            Image(
                painter = painterResource(id = R.drawable.ic_msg2),
                contentDescription = "",
                modifier = Modifier.constrainAs(icon) {
                    top.linkTo(parent.top, 5.dp)
                    end.linkTo(parent.end, 5.dp)
                    start.linkTo(edit.end, 5.dp)
                    bottom.linkTo(parent.bottom, 5.dp)
                }
            )
        }


        LazyRow(
            Modifier
                .fillMaxWidth()
        ) {
            items(list) {
                Column(
                    Modifier.clickable {
                        ARouter
                            .getInstance()
                            .build(ConstLive.LIVE_PATH)
                            .navigation()
                    },
                    verticalArrangement = Arrangement.Center,
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {
                    AsyncImage(
                        model = it.path,
                        contentDescription = "",
                        Modifier
                            .size(50.dp)
                            .padding(5.dp)
                            .clip(shape = CircleShape),
                        contentScale = ContentScale.Crop
                    )

                    Text(text = it.name)
                }
            }
        }

        Spacer(modifier = Modifier.height(10.dp))

        ConstraintLayout(Modifier.fillMaxWidth()) {
            val (title, title1) = createRefs()
            Text(
                text = "热门分类",
                color = Color.Blue,
                fontSize = 18.sp,
                modifier = Modifier.constrainAs(title) {
                    top.linkTo(parent.top)
                    bottom.linkTo(parent.bottom)
                    start.linkTo(parent.start, 5.dp)
                })

            Text(
                text = "查看所有",
                color = Color(0xFFE9B5B5),
                fontSize = 18.sp,
                modifier = Modifier.constrainAs(title1) {
                    top.linkTo(parent.top)
                    bottom.linkTo(parent.bottom)
                    end.linkTo(parent.end, 5.dp)
                }
            )
        }

        Spacer(modifier = Modifier.height(10.dp))

        Row(Modifier.fillMaxWidth()) {
            Column(
                Modifier.weight(1f),
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Image(
                    painter = painterResource(id = R.drawable.ic_game),
                    contentDescription = "",
                    Modifier.size(50.dp)
                )
                Text(text = "游戏区")
            }

            Column(
                Modifier.weight(1f),
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Image(
                    painter = painterResource(id = R.drawable.ic_music),
                    contentDescription = "",
                    Modifier.size(50.dp)
                )
                Text(text = "音乐区")
            }

            Column(
                Modifier.weight(1f),
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Image(
                    painter = painterResource(id = R.drawable.ic_life),
                    contentDescription = "",
                    Modifier.size(50.dp)
                )
                Text(text = "生活区")
            }

            Column(
                Modifier.weight(1f),
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Image(
                    painter = painterResource(id = R.drawable.ic_car),
                    contentDescription = "",
                    Modifier.size(50.dp)
                )
                Text(text = "汽车区")
            }
        }

        Spacer(modifier = Modifier.height(10.dp))

        Text(text = "热门直播", modifier = Modifier.padding(start = 5.dp))

        LazyVerticalGrid(columns = GridCells.Fixed(2), Modifier.fillMaxWidth()) {
            items(list) {
                Column(
                    Modifier
                        .fillMaxWidth()
                        .padding(5.dp),
                    verticalArrangement = Arrangement.Center,
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {
                    AsyncImage(
                        model = it.path,
                        contentDescription = "",
                        modifier = Modifier
                            .size(100.dp)
                            .clip(RoundedCornerShape(30))
                    )

                    Text(text = it.name, fontSize = 24.sp)
                }
            }
        }


    }
}

@RequiresApi(Build.VERSION_CODES.R)
@Preview(showBackground = true)
@Composable
fun showLive() {
    LivePage()
}